package com.club.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.club.subject.application.convert.SubjectAnswerDTOConverter;
import com.club.subject.application.convert.SubjectInfoDTOConverter;
import com.club.subject.application.dto.SubjectInfoDTO;
import com.club.subject.common.entity.PageResult;
import com.sfy.auth.entity.Result;
import com.club.subject.domain.entity.SubjectAnswerBO;
import com.club.subject.domain.entity.SubjectInfoBO;
import com.club.subject.domain.service.SubjectInfoDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: sfy
 * @Date: 2024/4/13 11:51
 * @Description: 题目controller
 */
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     *
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}",
                        JSON.toJSON(subjectInfoDTO));
            }
            //校验参数
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType()
                    , "题目类型不能为空(1单选 2多选 3判断 4简答)");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目得分不能为空");
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds())
                    , "题目分类不能为空");
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds())
                    , "标签分类不能为空");
            //将前端的DTO转为BO
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE
                    .convertListDTOToAnswerBO(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            //进行添加操作
            subjectInfoDomainService.add(subjectInfoBO);

            return Result.OK(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.Fail(e);
        }
    }


    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}",
                        JSON.toJSON(subjectInfoDTO));

            }
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            PageResult<SubjectInfoBO> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.OK(boPageResult);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.Fail(e);
        }
    }

    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}",
                        JSON.toJSON(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "题目标签不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "题目分类不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            SubjectInfoBO subjectInfoBO1 = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO res = SubjectInfoDTOConverter.INSTANCE.convertBOToDTO(subjectInfoBO1);
            return Result.OK(res);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.Fail(e);
        }
    }


    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            List<SubjectInfoBO> boList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> dtoList = SubjectInfoDTOConverter.
                    INSTANCE.convertDTOListToBOList(boList);
            return Result.OK(dtoList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.Fail(e);
        }
    }

}
